package com.hgyc.mom.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.util.poi.ExcelUtil;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.hgyc.mom.mes.entity.MesRouting;
import com.hgyc.mom.mes.service.MesRoutingService;
import com.hgyc.mom.mes.vo.MesRoutingParam;
import com.hgyc.mom.mes.vo.MesRoutingVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 工艺路线Controller
 *
 * @author fwj
 * @since 2025-07-01
 */
@Tag(name = "工艺路线控制器", description = "工艺路线控制器")
@RestController
@RequestMapping("/mes/routing")
public class MesRoutingController {
    @Autowired
    private MesRoutingService mesRoutingService;

    @Operation(summary = "查询工艺路线列表", description = "查询工艺路线列表")
    @PostMapping("/pageList")
    public Result<IPage<MesRouting>> pageList(@Parameter(description = "查询参数") @RequestBody MesRoutingParam mesRoutingParam) {
        Page<MesRouting> page = new Page<>(mesRoutingParam.getPage(), mesRoutingParam.getPageSize());
        MesRouting mesRouting = new MesRouting();
        BeanUtils.copyProperties(mesRoutingParam, mesRouting);
        QueryWrapper<MesRouting> wrapper = QueryWrapperUtil.build(mesRouting);
        IPage<MesRouting> list = mesRoutingService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取工艺路线详细信息", description = "获取工艺路线详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "工艺路线ID") @PathVariable("id") Long id) {
        return Result.success(mesRoutingService.getById(id));
    }
    @Operation(summary = "新增工艺路线", description = "新增工艺路线")
    @PostMapping
    public Result<?> save(@Parameter(description = "工艺路线") @RequestBody MesRoutingVO mesRoutingVO) {
        mesRoutingService.saveRouting(mesRoutingVO);
        return Result.success();
    }

    @Operation(summary = "修改工艺路线", description = "修改工艺路线")
    @PutMapping
    public Result<?> update(@Parameter(description = "工艺路线") @RequestBody MesRoutingVO mesRoutingVO) {
        mesRoutingService.updateRouting(mesRoutingVO);
        return Result.success();
    }

    @Operation(summary = "删除工艺路线", description = "删除工艺路线")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        mesRoutingService.removeRouting(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "更新状态", description = "更新状态")
    @PutMapping("/status/{status}")
    public Result<?> updateStatus(@PathVariable("status") String status, @RequestParam("ids") Long[] ids) {
        mesRoutingService.updateStatus(status, Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "获取产品工艺路线", description = "获取产品工艺路线")
    @GetMapping("/product/selectList/{productId}")
    public Result<List<MesRouting>> getProductRouting(@PathVariable("productId") Long productId) {
        List<MesRouting> routingList = mesRoutingService.getProductRouting(productId);
        return Result.success(routingList);
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MesRouting> util = new ExcelUtil<>(MesRouting.class);
        util.importTemplateExcel(response, "工艺路线");
    }

    @Operation(summary = "导出工艺路线列表", description = "导出工艺路线列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesRoutingParam mesRoutingParam) {
        MesRouting mesRouting = new MesRouting();
        BeanUtils.copyProperties(mesRoutingParam, mesRouting);
        QueryWrapper<MesRouting> wrapper = QueryWrapperUtil.build(mesRouting);
        List<MesRouting> list = mesRoutingService.list(wrapper);
        ExcelUtil<MesRouting> util = new ExcelUtil<>(MesRouting.class);
        util.exportExcel(response, list, "工艺路线数据");
    }

    @Operation(summary = "导入工艺路线列表", description = "导入工艺路线列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MesRouting> util = new ExcelUtil<>(MesRouting.class);
        List<MesRouting> mesRoutingList = util.importExcel(file.getInputStream());
        String message = mesRoutingService.importData(mesRoutingList, updateSupport);
        return Result.success(message);
    }
}
